package com.yitao.cms.service.order;

import com.skipper.base.service.GenericEntityService;
import com.yitao.client.dto.order.MallOrderDeliveryDto;
import com.yitao.client.model.order.MallOrderDeliveryModel;
import com.yitao.client.model.order.OrderDeliveryPackageModel;
import com.yitao.client.model.order.QMallOrderDeliveryModel;
import com.yitao.client.model.print.PrintRecodeModel;
import com.yitao.cms.service.print.PrintRecodeService;
import com.yitao.cms.utils.DateUtils;
import com.yitao.cms.vo.order.CmsOrderDeliveryVo;
import com.yitao.cms.vo.order.PrintRecodeVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import java.util.ArrayList;
import java.util.List;

@Service
public class CmsOrderDeliveryService extends GenericEntityService<MallOrderDeliveryModel, MallOrderDeliveryDto, QMallOrderDeliveryModel> {
    public CmsOrderDeliveryService(EntityManager entityManager) {
        super(entityManager);
    }

    @Autowired
    private OrderDeliveryPackageService orderDeliveryPackageService;
    @Autowired
    private PrintRecodeService printRecodeService;

    /**
     * 发货订单
     *
     * @param mallOrderDeliveryModel
     * @return
     */
    @Transactional
    public MallOrderDeliveryModel save(MallOrderDeliveryModel mallOrderDeliveryModel) {
        return repository.save(mallOrderDeliveryModel);
    }

    public List<MallOrderDeliveryModel>  getMallOrderDeliveryModelByOrderId(Long orderId) {
       return queryFactory.select(Q).from(Q).where(Q.orderId.eq(orderId).and(Q.isDelete.eq(Boolean.FALSE))).fetch();
    }

    /**
     * 根据订单ID查询
     */
    public List<CmsOrderDeliveryVo> getMallOrderDeliveryByOrderId(Long orderId) {
        List<CmsOrderDeliveryVo> cmsList = new ArrayList<>();
        List<MallOrderDeliveryModel> mallOrderDeliveryModelList = queryFactory.select(Q).from(Q).where(Q.orderId.eq(orderId)).fetch();
        if (mallOrderDeliveryModelList != null && mallOrderDeliveryModelList.size() != 0) {
            for (MallOrderDeliveryModel mallOrderDeliveryModel : mallOrderDeliveryModelList) {
                CmsOrderDeliveryVo cmsOrderDeliveryVo = new CmsOrderDeliveryVo();
                BeanUtils.copyProperties(mallOrderDeliveryModel, cmsOrderDeliveryVo);
                List<OrderDeliveryPackageModel> packageInfos = orderDeliveryPackageService.getOrderPackageInfo(mallOrderDeliveryModel.getId());
                if (packageInfos != null && packageInfos.size() > 0) {
                    List<PrintRecodeVo> list = new ArrayList<>();
                    for (OrderDeliveryPackageModel item : packageInfos) {
                        if (StringUtils.isNotBlank(item.getPackageCode())) {
                            list = printRecodeService.getPrintRecode(item.getPackageCode());
                        }
                        cmsOrderDeliveryVo.setPackageCode(item.getPackageCode());
                        cmsOrderDeliveryVo.setViewSwitch(false);
                        cmsOrderDeliveryVo.setLogisticsDetails(new ArrayList<>());
                        cmsOrderDeliveryVo.setWarehouseType(item.getWarehouseType() == null ? "无" : item.getWarehouseType() == 1 ? "仓库" : "采购车");
                        if (list != null && list.size() > 0) {
                            cmsOrderDeliveryVo.setPrintList(list);
                            cmsOrderDeliveryVo.setMachineCode(list.get(0).getMachineCode());
                        } else {
                            cmsOrderDeliveryVo.setPrintList(new ArrayList<>());
                            cmsOrderDeliveryVo.setMachineCode("无");
                        }
                    }
                } else {
                    cmsOrderDeliveryVo.setWarehouseType("无");
                    cmsOrderDeliveryVo.setViewSwitch(false);
                    cmsOrderDeliveryVo.setLogisticsDetails(new ArrayList<>());
                    cmsOrderDeliveryVo.setPrintList(new ArrayList<>());
                    cmsOrderDeliveryVo.setMachineCode("无");
                }
                if (mallOrderDeliveryModel.getCreateTime() != null) {
                    cmsOrderDeliveryVo.setCreateTime(DateUtils.getDateSecondFormat(mallOrderDeliveryModel.getCreateTime()));
                }
                cmsList.add(cmsOrderDeliveryVo);
            }
        }
        return cmsList;
    }

    public MallOrderDeliveryModel getMallOrderDeliveryById(Long deliveryId) {
        return queryFactory.select(Q).from(Q).where(Q.id.eq(deliveryId)).fetchOne();
    }
}
